package com.lsk.apipassenger.service;

import com.lsk.apipassenger.remote.ServicePassengerUserClient;
import com.lsk.apipassenger.remote.ServiceVerificationClient;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.CommonStatusEnum;
import com.lsk.internalcommon.constant.IdentityConstants;
import com.lsk.internalcommon.constant.TokenConstants;
import com.lsk.internalcommon.request.VerificationCodeDTO;
import com.lsk.internalcommon.response.NumberCodeResponse;
import com.lsk.internalcommon.response.TokenResponse;
import com.lsk.internalcommon.util.JwtUtils;
import com.lsk.internalcommon.util.RedisPrefixUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerificationCodeService {

    @Autowired
    private ServiceVerificationClient serviceVerificationClient;

    @Autowired
    private ServicePassengerUserClient servicePassengerUserClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取验证码
     * @param passengerPhone
     * @return
     */
    public ResponseResult generator(String passengerPhone){
        //调取验证码服务,获取验证码
        System.out.println("调取验证码服务,获取验证码");
        ResponseResult<NumberCodeResponse> responseResult = serviceVerificationClient.getNumberCode(6);
        int numberCode = responseResult.getData().getNumberCode();
        System.out.println("remote verification "+ numberCode);

        //存入到redis中
        System.out.println("存入到redis中");
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone,IdentityConstants.PASSENGER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key,numberCode+"",2, TimeUnit.MINUTES);

        //通过短信服务商，将验证码发送到手机

        return ResponseResult.success();
    }

    /**
     * 校验验证码
     * @param passengerPhone
     * @param verificationCode
     * @return
     */
    public ResponseResult check(String passengerPhone,String verificationCode){
        //从redis中获取到验证码
        String key = RedisPrefixUtils.generatorKeyByPhone(passengerPhone,IdentityConstants.PASSENGER_IDENTITY);
        String code = stringRedisTemplate.opsForValue().get(key);
        //进行校验验证码是否一致
        if (StringUtils.isBlank(code)){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_NOT_EXIST.getCode(),CommonStatusEnum.VERIFICATION_CODE_NOT_EXIST.getValue());
        }
        if (!verificationCode.trim().equals(code.trim())){
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        //校验用户是否存在，不存在就添加用户
        VerificationCodeDTO verificationCodeDTO = new VerificationCodeDTO();
        verificationCodeDTO.setPassengerPhone(passengerPhone);
        servicePassengerUserClient.loginOrRegister(verificationCodeDTO);
        //颁发令牌
        String accessToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY, TokenConstants.ACCESSTOKEN);
        String refreshToken = JwtUtils.generatorToken(passengerPhone, IdentityConstants.PASSENGER_IDENTITY,TokenConstants.REFRESHTOKEN);


        //把token存入到redis中
        String accessTokenKey = RedisPrefixUtils.tokenKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY,TokenConstants.ACCESSTOKEN);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);
        String refreshTokenKey = RedisPrefixUtils.tokenKeyByPhone(passengerPhone, IdentityConstants.PASSENGER_IDENTITY,TokenConstants.REFRESHTOKEN);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setAccessToken(accessToken);
        tokenResponse.setRefreshToken(refreshToken);

        return ResponseResult.success(tokenResponse);
    }


}
